package com.example.demo.Util;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtil {
    private static final ScheduledThreadPoolExecutor TASK_POOL = new ScheduledThreadPoolExecutor(3);
    // 定时任务,延迟执行,delay为延迟时间,单位毫秒
    public static void Scheruled(Runnable runnable, long delay){
        TASK_POOL.schedule(() -> {
            execute(runnable);
        }, delay, TimeUnit.MILLISECONDS);
    }
    // 线程池
    public static final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
    // 执行任务
    public static void execute(Runnable runnable)
    {
        threadPoolTaskExecutor.execute(runnable);
    }
    //线程池初始化
    public static void init()
    {
        threadPoolTaskExecutor.initialize();
    }
    // 关闭线程池
    public static void shutdown()
    {
        threadPoolTaskExecutor.shutdown();
    }
}
